package jemuillot.BiohazardPanelPuzzle;

import org.anddev.andengine.entity.layer.ILayer;
import org.anddev.andengine.entity.sprite.Sprite;

public class Light extends LogicalLight implements BiohazardPanelPuzzleConsts {

	MainGameScene mainGame;
	BiohazardPanelPuzzle bpp;

	private Sprite lightOn;
	private Sprite lightOff;
	private boolean visible;

	protected int idx;
	protected int idy;

	float lightDistX;
	float lightDistY;

	public Light(MainGameScene game, int maxtrix, int idx, int idy, float panelRgnCenterX,
			float panelRgnCenterY)
	{
		super(false);

		mainGame = game;
		bpp = game.bpp;

		this.idx = idx;
		this.idy = idy;

		visible = true;

		float startX;
		float startY;
		
		if (maxtrix == 3) {

			lightDistX = LIGHT_DIST_X;
			lightDistY = LIGHT_DIST_Y;

			startX = LIGHT_OFFSET_START_X + panelRgnCenterX;
			startY = LIGHT_OFFSET_START_Y + panelRgnCenterY;

		} else {

			lightDistX = LIGHTZONE_SIDE / maxtrix;
			lightDistY = lightDistX;

			startX = LIGHTX_STARTX;
			startY = LIGHTX_STARTY;
		}

		lightOn = new Sprite(startX + idx * lightDistX, startY + idy
				* lightDistY, bpp.lightOnRegion);
		
		lightOff = new Sprite(startX + idx * lightDistX, startY + idy
				* lightDistY, bpp.lightOffRegion);

		if (maxtrix > 3) {
			
			lightOn.setScaleCenter(0,0);
			lightOff.setScaleCenter(0,0);
			
			lightOn.setScale(lightDistX / lightOn.getWidth());
			lightOff.setScale(lightDistX / lightOff.getWidth());			
		}

		onStatusChange();
	}

	void addToLayer(ILayer layer) {
		layer.addEntity(lightOn);
		layer.addEntity(lightOff);
	}

	public void turnOn(boolean on) {
		super.turnOn(on);
		onStatusChange();
	}

	private void onStatusChange() {
		lightOn.setVisible(visible && lighting);
		lightOff.setVisible(visible && !lighting);
	}

	public void toggle() {
		super.toggle();
		onStatusChange();
	}

	public void setVisible(boolean v) {
		visible = v;
		onStatusChange();
	}
}
